import { REACT_ELEMENT_TYPE } from "shared/ReactSymbols";
import hasOwnProperty from "shared/hasOwnProperty";// 引用目录太长 所以配置文件别名

const RESERVED_PROPS = {
  key: true,
  ref: true,
  __self: true,
  __source: true

}
// 构建虚拟DOM
export function jsxDEV(type, config) {
  let propsName;
  const props = {}
  let key = null
  let ref = null
  if (hasValidKey(config)) {
    key = config.key
  }
  if (hasValidRef(config)) {
    ref = config.ref
  }
  // 把config 对象中的非保留属性赋值给props
  for (propsName in config) {
    if (hasOwnProperty.call(config, propsName) && !RESERVED_PROPS.hasOwnProperty(propsName)) {
      props[propsName] = config[propsName]
    }
  }
  return ReactElement(type, key, ref, props)
}
function ReactElement(type, key, ref, props) {
  return {
    $$typeof: REACT_ELEMENT_TYPE,
    type,// h1 span
    key,// 区分节点
    ref,// 用来获取真实DOM
    props
  }
}
function hasValidKey(config) {
  return config.key !== undefined
}
function hasValidRef(config) {
  return config.ref !== undefined
}